package com.czapl.tabs;

import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.czapl.api.ITower;
import com.czapl.api.ITower.ITowerFromMenuSelectionListener;
import com.czapl.mygdxgame.MyGdxGame;


public class BuildTab extends CTab {

	private ITower m_selectedTower = null;
	private ITowerFromMenuSelectionListener m_selectionListener;
	
	public BuildTab(ITowerFromMenuSelectionListener listener) {
		m_selectionListener = listener;
		TextButtonStyle buttonStyle = MyGdxGame.skin.get("default", TextButtonStyle.class);
        for(int i=0; i<MyGdxGame.AVAILABLE_TOWER_COUNT;i++) {
        	TextButton btn1 = new TextButton("t", buttonStyle);
        	add(btn1).size(UIDefender.MENU_WIDTH/3, 50);
        	if(0==(i+1)%3) {
        		row();
        	}
            btn1.addListener( new TowerClickListener(i));
        }
	}
	
	private class TowerClickListener extends ClickListener {
		int m_index;
		private TowerClickListener(int index) {
			m_index = index;
		}
		 @Override
         public void clicked(InputEvent event,float x,float y )
         {
         	ITower tower = MyGdxGame.TowerInstances.get(m_index);
    		if(m_selectedTower != null) {
    			m_selectedTower = null;
    		}
    		else {
    			m_selectedTower = tower;
    		}
         	m_selectionListener.onTowerSelected(tower);
         } 
	}
}
